package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import entity.tUser;
import service.tUserService;

public class DeletetUserAction {
	
	private String userId;
	private String result;
	
	private tUserService tuserService;
	
	@SuppressWarnings("unchecked")
	public String delete() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    @SuppressWarnings("unused")
		HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		
		System.out.println(userId);
		
		List<tUser> tuser = new ArrayList<tUser>();
		
		tuser = tuserService.searchById(userId);
		
		Integer grade = tuser.get(0).getGrade();
		
		System.out.println(grade);
		
		if (grade == 2){
			result = "您无法删除管理员用户！";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			
			return "error";
		}

		
		tuserService.delete(userId);
		System.out.println("------已删除--------");
		return "success";
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	

}
